package com.jsb.laundry.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jsb.laundry.common.utils.http.HttpUtil;
import com.jsb.laundry.model.entity.WxSend;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class WxSendServiceImpl {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxSendServiceImpl.class);

    /**
     * 公众号推送
     */
    public static void send(WxSend wxSend) {
        /**
         * 创建一个post请求
         * 获取手机号
         */
        String appId = null;
        String appSecret = null;
        //骑手
        if (wxSend.getStatus() == 0) {
            appId = "wx66259c2db83de8c2";
            appSecret = "a751a2d916092083cbd85d4f58ffc0b5";
        }
        //用户
        if (wxSend.getStatus() == 1) {
            appId = "wxb730137b1ae9f679";
            appSecret = "86fcb97161a0ea96bc082f3b58d0e8ed";
        }
        String accessToken = accessToken(appId, appSecret);
        CloseableHttpClient client11 = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken);
        JSONObject jsonb = new JSONObject();

        JSONObject jsonbs = new JSONObject();
        JSONObject character_string1 = new JSONObject();
        character_string1.put("value", wxSend.getTime10());
        JSONObject thing2 = new JSONObject();
        thing2.put("value", wxSend.getThing2());
        JSONObject amount3 = new JSONObject();
        amount3.put("value", wxSend.getAmount8());
        JSONObject amount26 = new JSONObject();
        amount26.put("value", wxSend.getAmount26());
        JSONObject thing4 = new JSONObject();
        thing4.put("value", wxSend.getThing9());
        JSONObject thing5 = new JSONObject();
        thing5.put("value", wxSend.getCharacter_string22());

        jsonbs.put("time10", character_string1);
//        jsonbs.put("thing2",thing2 );
        jsonbs.put("amount8", amount3);
        jsonbs.put("amount26", amount26);
//        jsonbs.put("thing9", thing4);
        jsonbs.put("character_string22", thing5);
        jsonb.put("data", jsonbs);

        jsonb.put("touser", wxSend.getOpenId());
        jsonb.put("miniprogram_state", wxSend.getMiniprogram_state());
        jsonb.put("lang", wxSend.getLang());
        jsonb.put("template_id", wxSend.getTemplateId());
        StringEntity postingString = new StringEntity(jsonb.toString(), "UTF-8");
        post.setEntity(postingString);
        post.setHeader("Content-type", "application/json");
        HttpResponse response = null;
        try {
            response = client11.execute(post);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String content = null;
        try {
            content = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JSONObject ss = JSON.parseObject(content);
        LOGGER.info("订阅返回值；{}", ss);
        LOGGER.info("json字符串:{}", jsonb);
        LOGGER.info("用户推送的数据为------------：{}", jsonbs);
    }

    public static String accessToken(String appId, String appSecret) {
        String s = HttpUtil.sendGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appId=" + appId + "&secret=" + appSecret, null, null, null);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String accessToken = jsonObject.getString("access_token");
        return accessToken;
    }

    //推送骑手
    public static void sendLaundry(JSONObject data, String openId) {
        JSONObject object = new JSONObject();

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("appid", "wx66259c2db83de8c2");

        object.put("touser", openId);
        object.put("template_id", "UeSbOHkNikaNkof_egGr-Nm2rAJ7ag_18rT1TOEVm4A");
        object.put("miniprogram", jsonObject1);
        object.put("data", data);

        String token = accessToken("wxa808024765c753cb", "7333cc2d01b75703f350f4a1aa6cdf4d");


        CloseableHttpClient client11 = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token);
        StringEntity postingString = new StringEntity(object.toString(), "UTF-8");
        post.setEntity(postingString);
        post.setHeader("Content-type", "application/json");
        HttpResponse response = null;
        try {
            response = client11.execute(post);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String content = null;
        try {
            content = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JSONObject ss = JSON.parseObject(content);
        LOGGER.info("推送骑手:{}", ss);
    }

}
